import { _decorator, Component, Node, instantiate } from 'cc';
import { EnEvent } from '../Common/const/EnEvent';
import { GameAPI } from '../Common/GameAPI';
const { ccclass, property } = _decorator;

export class EnergyDataInfo {
    public energyObj: Node = null;
    public num: number = 0;
}

@ccclass('PlayerEnergyManager')
export class PlayerEnergyManager extends Component {

    @property(Node)
    public treeParentNode: Node = null;
    @property(Node)
    private treePrefab: Node = null;
    @property(Node)
    public rouParentNode: Node = null;
    @property(Node)
    private rouPrefab: Node = null;

    private tipRouNum: number = 20;

    private energyRouData: Array<EnergyDataInfo> = [];
    private energyTreeData: Array<EnergyDataInfo> = [];

    start() {
        GameAPI.RegGEvent(EnEvent.PlayerEnergyAdd, this.OnPlayerEnergyAdd.bind(this), this);
        GameAPI.RegGEvent(EnEvent.PlayerEnergyReduce, this.OnPlayerEnergyReduce.bind(this), this);
    }

    onDestroy() {
        GameAPI.OffAllCaller(this);
    }

    IsCreate(data, maxNum) {
        if (data.length == 0) {
            return true;
        }
        let lastData = data[data.length - 1];
        if (lastData.num < maxNum) {
            data[data.length - 1].num++;
            return false;
        }
        else {
            return true;
        }
    }

    OnPlayerEnergyAdd(arg) {
        for (let index = 0; index < arg.num; index++) {
            let obj = null;
            let result = false;
            if (arg.type == 1) {
                result = this.IsCreate(this.energyTreeData, 2);
                if (result) {
                    obj = this.CreatorChatItem(this.treePrefab, this.treeParentNode);
                    let info = new EnergyDataInfo();
                    info.energyObj = obj;
                    info.num = 1;
                    this.energyTreeData.push(info);
                }
            }
            else {
                result = this.IsCreate(this.energyRouData, 5);
                if (this.tipRouNum == 0) {
                    GameAPI.FireGEvent(EnEvent.GuideTipEvent, { index: 2, active: false });
                    GameAPI.FireGEvent(EnEvent.GuideTipEvent, { index: 4, active: true });
                }
                else {

                }
                this.tipRouNum -= 1;
                if (result) {
                    obj = this.CreatorChatItem(this.rouPrefab, this.rouParentNode);
                    let info = new EnergyDataInfo();
                    info.energyObj = obj;
                    info.num = 1;
                    this.energyRouData.push(info);
                }
            }
            // console.log(this.energyTreeData.length);
            // console.log(this.energyRouData.length);
        }
    }

    OnPlayerEnergyReduce(energyType) {
        if (energyType == 1) {
            this.RemoveEnergy(energyType, this.treeParentNode, this.energyTreeData);
        }
        else {
            this.RemoveEnergy(energyType, this.rouParentNode, this.energyRouData);
        }
    }

    CreatorChatItem(prefab, parent) {
        let obj = instantiate(prefab);
        parent.addChild(obj);
        obj.active = true;
        return obj;
    }

    RemoveEnergy(type, parentNode, energyData) {
        if (parentNode.children.length > 0) {
            if (energyData.length > 0) {
                let energyInfo = energyData[0];
                let num = energyInfo.num;
                num--;
                if (num <= 0) {
                    if (energyInfo.energyObj) {
                        energyInfo.energyObj.destroy();
                    }
                    energyData.splice(0, 1);
                }
                else {
                    energyData[0].num = num;
                }
            }
        }
    }

    GetEnergyAllNum(energyData) {
        let num = 0;
        for (let index = 0; index < energyData.length; index++) {
            const element = energyData[index];
            num += element.num;
        }
        return num;
    }

    GetEnergyNumByType(type) {
        let num = 0;
        if (type == 1) {
            num = this.GetEnergyAllNum(this.energyTreeData);
        }
        else {
            num = this.GetEnergyAllNum(this.energyRouData);
        }
        return num;
    }

    RemoveEnergyNumByType(type, removeNum = 1) {
        if (type == 1) {
            this.RemoveEnergy(type, this.treeParentNode, this.energyTreeData);
        }
        else {
            for (let index = 0; index < removeNum; index++) {
                this.RemoveEnergy(type, this.rouParentNode, this.energyRouData);
            }
        }
    }

}


